package com.gulimall.order.utils;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private String app_id = "2021000121644135";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCqydEJEKi" +
            "+CY0V8yi7pDHZ6UH+cvWu952tJPFPoyDsrwyLPitDcF1UT8FFVfLq4mBbj9c6Wdfbwj909yv5eh1oizAnr4XtILf+SNegwtDyhS" +
            "+XestDNFgFLykqFhtP+TpTQKFYGMvcUqDws2qCvVDsjqAxRFvagO2on3QWNGrEZi0Vj7A3S6jKKm6OwOMfaDxxDaQd2q/aXRzkuXrqaq"
            + "+6j0vJaOoYfdE4X/Vsk83xK2FfT8VS9ENu2qpRFqB401ZCwiWeFCm5YbwZXKoiV" +
            "+oNAiOpzobgZztsVAK3K61Zp4Ew0gbEJUgD166SI3ClajTDvn4h0mKSX5H4Mh0MSg1rAgMBAAECggEAXHiBMcNPWfWZ6jSJ7W7tfusIzFg6Jc30FXdXIKNWhFATHqE+k3kXTzsPjhcHO53uRiUPh4x1j4sRCKd7q8wueBhErnger1Fzzb5q4aQZtfXe2smlnHGO5t+uDheCGj77EXCA604hst0aPrkmBjuywSJIIeS9RwVfqFkNZahj1ooHjThA3tuEN/BBk/IzLJGVWpN7XNM98rdbzuWl4z1XYltzQVxFtTlZszZ3AmyJCzMu7mp3zgeBlmQPhcdxT7EEIfuEbYn/sV9q9AI1a4onxHOqP6u4kyO7DL+igCo8aPcNCf97QZcMib+kwA2VJiYihP/2f7jBx5vk7Xla7SlmoQKBgQDvKyz1Rw7stZFYyFvxAqrnuJrVx7W1XyuBnNLTYctSdhmkIyFGYSatBj9XSF2+WFMdvuVr1Inc6yRppT3j+n6RXB1Q8iK2r2jukyO+fRXTdh3AxSB8RoIohb8md20A4AwyqU3gRUY+KsrYVnjhYj2uU3OAyjpiRMhkf7CkHiBqNQKBgQC2zrZRJrCMf1cw7dDpAILK+tDl8LCEHsB+WUq6hBGJbCXZxSlQ/4VwAH2GU75j7BlEGw1aU71P/dPRfycmd3I0pYrmGCS4pi/j+uMdeIf8zOPCw70TH1x5Bqb9F0fNnB/z77CrWbbaSLNjPF7xWlolG0m5NeSUZmzeKKr3qCaNHwKBgQCom62UNUYmRlJzXrzCifjmZ1RwxdwqQy+urlpKvzTRq4SkHEgHD9JuRGs/IztDR41A+SVQAYQ3hGYlPGMHZ772xxO7zxf95md5Wa3nStOqQijzj4U5V5PG8ZMykRr9dPXkeUr3gp7kkNJz9XI+oj2NujNBZlAGmysS/poO+VYsyQKBgFfSHowFxjL4ye5bkU0eTbctn0WXx3TxQRBx3RCVecdHCTgBG8QpvPT9h1AM0x41wj2RsWx/ZicTjbrLFwfHwjnAfWXkm1+JluAzGwCBcvLji1d5qU+SdV4+7+IOWRRRuJf7wifPRaS/4E4AJqxT95Jz9YrgdRtFyW9AokGvqjWJAoGARnUq622SVJyxsKJPEmIKwgCm8sCwO1/meYJq2GnjpgDzFle79rOvQiaCH6cf8vNshUmvROBmFwZfxY8V/JxRmrsaK+SGB1jXAsSQBCVTinDFVfI1YjHMqxE5phyYo329jvVngT7cihGEI4nGMEaNeINwgbZH+9dTp/z0ZiDWJuE=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm
    // 对应APPID下的支付宝公钥。
    private String alipay_public_key =
            "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAkV1X4H1qiB0FlCF61kUuROAugxv1CSJNw81W" +
                    "+rOyU5DnfZAiDVUu0VboXcWTIWcZYiEuLBV8ikQWMKx0fap" +
                    "+sWS6yt55gBVSnnRebBh91SvtGWFMNRsN5y6Q5lDlSsqT8Nub8kJ7LwdFo7rxcom8Bpw1pmaMFzBgXaMO5PXRFLq0wPcWvb9HDJa/dSIxjLSZJiB7J4625KNY6gfJs/zr1d7FDayB8p1C/x8kYkHRWUEx0f2+w1Tga3ziDdQf4uNyuOPro+j8PhFzfy48iu8FETk6k6YSUwSgvCYdkTry2nq//1bhJfK2B3XBsLlV8QcmVrtGOr7UiYb+bue7fdkJ/wIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private String notify_url = "http://0fs3g94c.dongtaiyuming.net:15066/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private String return_url = "http://order.gulimall.com/order.html";

    // 签名方式
    private String sign_type = "RSA2";

    // 字符编码格式
    private String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id,
        // AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key,
        // AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl, app_id, merchant_private_key, "json", charset
                , alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String outTradeNo = vo.getOut_trade_no();
        //付款金额，必填
        String totalAmount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\"" + outTradeNo + "\"," + "\"total_amount\":\"" + totalAmount + "\"," + "\"subject\":\"" + subject + "\"," + "\"body\":\"" + body + "\"," + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应：" + result);

        return result;

    }
}
